package com.desaysv.wandroid.ui.main

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.MessageQueue
import android.provider.Settings
import android.view.View
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.core.content.ContextCompat
import com.desaysv.wandroid.R
import com.desaysv.wandroid.base.BaseActivity
import com.desaysv.wandroid.databinding.ActivityBaseBinding
import com.desaysv.wandroid.databinding.ActivityMainBinding
import com.desaysv.wandroid.fragment.ClassicFragment
import com.desaysv.wandroid.fragment.MineFragment
import com.desaysv.wandroid.fragment.SquareFragment
import com.desaysv.wandroid.fragment.main.MainFragment
import com.desaysv.wandroid.projection.ProjectionActivity
import com.desaysv.wandroid.ui.main.mvp.MainContract
import com.desaysv.wandroid.ui.main.mvp.MainModel
import com.desaysv.wandroid.ui.main.mvp.MainPresenter
import com.desaysv.wandroid.utils.ProjectionService
import com.next.easynavigation.view.EasyNavigationBar

class MainActivity : BaseActivity<MainContract.MainView, MainModel, MainPresenter>(),
    MainContract.MainView {
    private val TAG = "MainActivity"
    private lateinit var mMainBinding: ActivityMainBinding
    private var currenPage = 0
    private val mNativeTitle = arrayOf("首页", "广场", "教程", "我的")
    private val mNativeUnChooseIcons = intArrayOf(
        R.drawable.icon_main_unchoose,
        R.drawable.icon_square_unchoose,
        R.drawable.icon_classic_unchoose,
        R.drawable.icon_mine_unchoose
    )
    private val mNativeChooseIcons = intArrayOf(
        R.drawable.icon_main_choose,
        R.drawable.icon_square_choose,
        R.drawable.icon_classic_choose,
        R.drawable.icon_mine_choose
    )
    private val mFragments = listOf(
        MainFragment.newInstance(),
        SquareFragment.newInstance(),
        ClassicFragment.newInstance(),
        MineFragment.newInstance()
    )

    override fun setView(): View {
        mMainBinding = ActivityMainBinding.inflate(layoutInflater)
        return mMainBinding.root
    }

    override fun init(base: ActivityBaseBinding) {
//        noTitle()
//        noBack()
//        noAdd()
        changeStatusBar(R.color.black2)
        initNav()
    }

    override fun back() {
        finish()
    }

    override fun add() {
        if (ProjectionService.requestPermission(this)) {
            ProjectionService.start(this,null)
        }else{
            this?.startActivityForResult(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + this?.packageName)), 10001)
        }
    }

    override fun initNav() {
        mMainBinding.apply {
            navigationBar
                .fragmentManager(supportFragmentManager)
                .titleItems(mNativeTitle)
                .normalIconItems(mNativeUnChooseIcons)
                .selectIconItems(mNativeChooseIcons)
                .fragmentList(mFragments)
                .mode(EasyNavigationBar.NavigationMode.MODE_NORMAL)
                .canScroll(true)
                .build()
        }
    }

    override fun checkPermission() {

        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.RECORD_AUDIO
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_SECURE_SETTINGS,
                    Manifest.permission.RECORD_AUDIO
                ),
                10001
            )
        }
    }

    override fun setPresenter(): MainPresenter {
        return MainPresenter()
    }

}